<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>画图板</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        canvas {
            border: 10px solid darkorange;
        }

        input {
            margin-left: 450px;
        }
    </style>
</head>

<body>

    <canvas id="mycanvas"></canvas><br>
    <input type="color" id="selectInput">
    <button id="clearBtn">清除</button>

    <script>
        let mycanvas = document.getElementById('mycanvas');
        mycanvas.width = 500;
        mycanvas.height = 500;
        // 创建上下文对象
        let ctx = mycanvas.getContext('2d');
        ctx.beginPath();
        ctx.lineWidth = 10;
        ctx.lineCap = "round";
        ctx.lineJoin = "round";
        ctx.strokeStyle = 'acyn';
        ctx.stroke();

        let clearBtn = document.getElementById('clearBtn');
        clearBtn.onclick = function () {
            // 清除画板  clearRect(x,y,width,height);
            ctx.clearRect(0, 0, mycanvas.width, mycanvas.height);
        }

        let selectInput = document.getElementById('selectInput');
        let flag = false;
        let lastPoint = {
            x: 0,
            y: 0
        }
        let curPoint = {
            x: 10,
            y: 10
        }
        // 鼠标按下
        mycanvas.onmousedown = function (e) {
            flag = true;
            ctx.beginPath();
            ctx.strokeStyle = selectInput.value;
            ctx.lineTo(e.clientX, e.clientY);
        }
        // 鼠标移动
        mycanvas.onmousemove = function (e) {
            if (flag) {
                ctx.lineTo(e.clientX - 10, e.clientY - 10);
                ctx.stroke();
            }
        }
        // 鼠标松开
        mycanvas.onmouseup = function (e) {
            flag = false;
        }
        // 鼠标移除
        mycanvas.onmouseleave = function (e) {
            flag = false;
        }

    </script>
</body>

</html>